#include <stdio.h>
#include "controller.h"
#include "panel.h"
#include "button.h"
#include <string.h>
#include "sensor.h"
#include "relay.h"
#include "lmexxx_conf.h"
#include "MseProcess.h"
#include "eeprom.h"

const Controller ctrlerDefault = {
    .reg = {
        {REG_TEMP_SMP,              25},
        {REG_VALVE_STATE,           0},
        {REG_SPEED_OUT_AC,          SPEED_OUT_OFF},
        {REG_SPEED_OUT_FRESH,       SPEED_OUT_OFF},
        {REG_POWER_STATE,           0},
        {REG_MODE_AC,               ACMODE_COOL},
        {REG_TEMP_SET_AC,           25},
        {REG_TEMP_SET_DN,           25},
        {REG_SPEED_SET_AC,          SPEED_SET_AUTO},
        {REG_SPEED_SET_FRESH,       SPEED_SET_LOW},
        {REG_BTN_LOCK,              0},
        {REG_LOW_TEMP_PROTECT,      0},
        {REG_ECO,                   0},
        {REG_SPEED_CTRL,            1},
        {REG_MEMORY,                1},
        {REG_CALIB,                 0},
        {REG_BACKLASH_UP,           1},
        {REG_BACKLASH_DOWN,         0x81},
        {REG_TEMP_SET_LIMIT_UP,     30},
        {REG_TEMP_SET_LIMIT_DOWN,   16},
        {REG_ACMODE_SELECT,         0x07},
        {REG_TEMP_UNIT,             UNIT_C},
        {REG_SYSTEM,                SYSTEM_AC},
        {REG_SYSTEM_SELECT,         SYSTEM_AC},
    },
    .deviceAddr = 1,
    .baudrate = BAUDRATE_9600,
    .page = PAGE_SYSTEM,
    .luminance = 8,
    .lowPower = 0,
    .timer = 0,
    .relayMode = RELAY_MODE_AC,
    .net = 0,
    .btnLight = {1, 1, 1},
    .acStep = 0,
    .dnStep = 0,
    .speedDelay = SPEED_OUT_FOREVER,
    .menu = MENU_CALIB,
    .fault = 0,
    .save = 0,
};

static Controller ctrler;
/* private function prototype ----------------------------------------------- */

/* public function prototype ------------------------------------------------ */

void sys_params_deinit(void)
{
    memcpy(&ctrler, &ctrlerDefault, sizeof(ctrlerDefault));
    storage_params_save(&ctrler);
    set_unit(ctrler.reg[REG_TEMP_UNIT].val);
}

void sys_params_init(void)
{
    // delay_ms(1000);
    memcpy(&ctrler, &ctrlerDefault, sizeof(ctrlerDefault));
    
    if(storage_params_check())
    {
        storage_params_read(&ctrler);
    }
    else
    {
        sys_params_deinit();
    }
    if(ctrler.reg[REG_MEMORY].val == 0)
    {
        ctrler.reg[REG_MODE_AC].val = ctrlerDefault.reg[REG_MODE_AC].val;
        ctrler.reg[REG_TEMP_SET_AC].val = ctrlerDefault.reg[REG_TEMP_SET_AC].val;
        ctrler.reg[REG_TEMP_SET_DN].val = ctrlerDefault.reg[REG_TEMP_SET_DN].val;
        ctrler.reg[REG_SPEED_SET_AC].val = ctrlerDefault.reg[REG_SPEED_SET_AC].val;
        ctrler.reg[REG_SPEED_SET_FRESH].val = ctrlerDefault.reg[REG_SPEED_SET_FRESH].val;
        ctrler.reg[REG_TEMP_UNIT].val = ctrlerDefault.reg[REG_TEMP_UNIT].val;
        ctrler.reg[REG_SYSTEM].val = ctrlerDefault.reg[REG_SYSTEM].val;
        if(ctrler.reg[REG_TEMP_SET_LIMIT_UP].val > 35)
        {
            ctrler.reg[REG_TEMP_SET_LIMIT_UP].val = (ctrler.reg[REG_TEMP_SET_LIMIT_UP].val - 32) / 1.8f;
            ctrler.reg[REG_TEMP_SET_LIMIT_DOWN].val = (ctrler.reg[REG_TEMP_SET_LIMIT_DOWN].val - 32) / 1.8f;
        }
    }   
}

void sys_params_save(void)
{
    if(ctrler.save != 0)
    {
        ctrler.save = 0;
        storage_params_save(&ctrler);
    }
}

void set_page(uint8_t page)
{
    if(ctrler.page != page)
    {
        ctrler.page = page;
    }
}
void set_device_addr(uint8_t addr)
{
    if(ctrler.deviceAddr != addr)
    {
        ctrler.deviceAddr = addr;
    }
}
void set_baudrate(uint8_t baudrate)
{
    if(ctrler.baudrate != baudrate)
    {
        ctrler.baudrate = baudrate;
    }
}
void set_luminance(uint8_t luminance)
{
    if(ctrler.luminance != luminance)
    {
        ctrler.luminance = luminance;
    }
}
void set_low_Power(uint8_t state)
{
    if(ctrler.lowPower != state)
    {
        ctrler.lowPower = state;
    }
    if(state == 0)
    {
        ctrler.timer = 0;
    }
}
void set_relay_mode(uint8_t mode)
{
    if(ctrler.relayMode != mode)
    {
        ctrler.relayMode = mode;
    }
}
void set_net(uint8_t state)
{
    if(ctrler.net != state)
    {
        ctrler.net = state;
    }
}
void set_lock(uint8_t state)
{
    if(ctrler.reg[REG_BTN_LOCK].val != state)
    {
        ctrler.reg[REG_BTN_LOCK].val = state;
    }
}
void set_system(uint8_t type)
{
    if(ctrler.reg[REG_SYSTEM].val != type)
    {
        ctrler.reg[REG_SYSTEM].val = type;
    }
}
void set_power_ac(uint8_t state)
{
    if(state != 0)
    {
        if((ctrler.reg[REG_POWER_STATE].val & BIT_POWER_AC) == 0)
        {
            ctrler.reg[REG_POWER_STATE].val |= BIT_POWER_AC;
        }
    }
    else
    {
        if((ctrler.reg[REG_POWER_STATE].val & BIT_POWER_AC) != 0)
        {
            ctrler.reg[REG_POWER_STATE].val &= ~BIT_POWER_AC;
        }
    }
}
void set_controller_ac_step(uint8_t step)
{
    if(ctrler.acStep != step)
    {
        ctrler.acStep = step;
    }
}
void reset_controller_ac(void)
{
    if(ctrler.acStep != 0)
    {
        ctrler.acStep = 0;
    }
    set_output_speed_delay(SPEED_OUT_FOREVER);
}
void set_output_speed_delay(uint32_t delay)
{
    if(ctrler.speedDelay != delay)
    {
        ctrler.speedDelay = delay;
    }
}
void set_ac_mode(uint8_t mode)
{
    if(ctrler.reg[REG_MODE_AC].val != mode)
    {
        ctrler.reg[REG_MODE_AC].val = mode;
    }
}
void set_speedSet_ac(uint8_t speed)
{
    if(ctrler.reg[REG_SPEED_SET_AC].val != speed)
    {
        ctrler.reg[REG_SPEED_SET_AC].val = speed;
    }
}
void set_eco(uint8_t state)
{
    if(ctrler.reg[REG_ECO].val != state)
    {
        ctrler.reg[REG_ECO].val = state;
    }
}
void set_tempSet_ac(uint8_t temp)
{
    if(ctrler.reg[REG_TEMP_SET_AC].val != temp)
    {
        ctrler.reg[REG_TEMP_SET_AC].val = temp;
    }
}
void set_temp_upLimit(uint8_t temp)
{
    if(ctrler.reg[REG_TEMP_SET_LIMIT_UP].val != temp)
    {
        ctrler.reg[REG_TEMP_SET_LIMIT_UP].val = temp;
    }
}
void set_temp_downLimit(uint8_t temp)
{
    if(ctrler.reg[REG_TEMP_SET_LIMIT_DOWN].val != temp)
    {
        ctrler.reg[REG_TEMP_SET_LIMIT_DOWN].val = temp;
    }
}
void set_valve_ac(uint8_t state)
{
    if(state != 0)
    {
        if((ctrler.reg[REG_VALVE_STATE].val & BIT_VALVE_AC) == 0)
        {
            ctrler.reg[REG_VALVE_STATE].val |= BIT_VALVE_AC;
        }
    }
    else
    {
        if((ctrler.reg[REG_VALVE_STATE].val & BIT_VALVE_AC) != 0)
        {
            ctrler.reg[REG_VALVE_STATE].val &= ~BIT_VALVE_AC;
        }
    }
}
void set_valve_dn(uint8_t state)
{
    if(state != 0)
    {
        if((ctrler.reg[REG_VALVE_STATE].val & BIT_VALVE_DN) == 0)
        {
            ctrler.reg[REG_VALVE_STATE].val |= BIT_VALVE_DN;
        }
    }
    else
    {
        if((ctrler.reg[REG_VALVE_STATE].val & BIT_VALVE_DN) != 0)
        {
            ctrler.reg[REG_VALVE_STATE].val &= ~BIT_VALVE_DN;
        }
    }
}
void set_tempSmp(uint8_t temp)
{
    if(ctrler.reg[REG_TEMP_SMP].val != temp)
    {
        ctrler.reg[REG_TEMP_SMP].val = temp;
    }
}
void set_out_speed_ac(uint8_t speed)
{
    if(ctrler.reg[REG_SPEED_OUT_AC].val != speed)
    {
        ctrler.reg[REG_SPEED_OUT_AC].val = speed;
    }
}
void set_btn_light(uint8_t id, uint8_t state)
{
    if(ctrler.btnLight[id] != state)
    {
        ctrler.btnLight[id] = state;
    }
}
void set_menu(uint8_t menu)
{
    if(ctrler.menu != menu)
    {
        ctrler.menu = menu;
    }
}
void set_calib_unsigned(uint8_t uCalib)
{
    if(ctrler.reg[REG_CALIB].val != uCalib)
    {
        ctrler.reg[REG_CALIB].val = uCalib;
    }
}
void set_calib_signed(signed char calib)
{
    uint8_t uCalib;
    if(calib < 0)
    {
        uCalib = 0x80 | ((-calib) & 0x7F);
    }
    else
    {
        uCalib = calib;
    }
    if(ctrler.reg[REG_CALIB].val != uCalib)
    {
        ctrler.reg[REG_CALIB].val = uCalib;
    }
}
void set_speed_ctrl(uint8_t state)
{
    if(ctrler.reg[REG_SPEED_CTRL].val != state)
    {
        ctrler.reg[REG_SPEED_CTRL].val = state;
    }
}
void set_low_temp_protect(uint8_t state)
{
    if(ctrler.reg[REG_LOW_TEMP_PROTECT].val != state)
    {
        ctrler.reg[REG_LOW_TEMP_PROTECT].val = state;
    }
}
void set_memory_mode(uint8_t state)
{
    if(ctrler.reg[REG_MEMORY].val != state)
    {
        ctrler.reg[REG_MEMORY].val = state;
    }
}
void set_backlash_up(uint8_t val)
{
    if(ctrler.reg[REG_BACKLASH_UP].val != val)
    {
        ctrler.reg[REG_BACKLASH_UP].val = val;
    }
}
void set_backlash_down_unsigned(uint8_t uVal)
{
    if(ctrler.reg[REG_BACKLASH_DOWN].val != uVal)
    {
        ctrler.reg[REG_BACKLASH_DOWN].val = uVal;
    }
}
void set_backlash_down_signed(signed char val)
{
    uint8_t uVal;
    if(val < 0)
    {
        uVal = 0x80 | ((-val) & 0x7F);
    }
    else
    {
        uVal = val;
    }
    if(ctrler.reg[REG_BACKLASH_DOWN].val != uVal)
    {
        ctrler.reg[REG_BACKLASH_DOWN].val = uVal;
    }
}
void set_tempSet_upLimit(uint8_t val)
{
    if(ctrler.reg[REG_TEMP_SET_LIMIT_UP].val != val)
    {
        ctrler.reg[REG_TEMP_SET_LIMIT_UP].val = val;
    }
}
void set_tempSet_downLimit(uint8_t val)
{
    if(ctrler.reg[REG_TEMP_SET_LIMIT_DOWN].val != val)
    {
        ctrler.reg[REG_TEMP_SET_LIMIT_DOWN].val = val;
    }
}
void set_ac_cool_enable(uint8_t state)
{
    if(state != 0)
    {
        if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_COOL) == 0)
        {
            ctrler.reg[REG_ACMODE_SELECT].val |= BIT_ACMODE_COOL;
        }
    }
    else
    {
        if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_COOL) != 0)
        {
            ctrler.reg[REG_ACMODE_SELECT].val &= ~BIT_ACMODE_COOL;
        }
    }
}
void set_ac_heat_enable(uint8_t state)
{
    if(state != 0)
    {
        if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_HEAT) == 0)
        {
            ctrler.reg[REG_ACMODE_SELECT].val |= BIT_ACMODE_HEAT;
        }
    }
    else
    {
        if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_HEAT) != 0)
        {
            ctrler.reg[REG_ACMODE_SELECT].val &= ~BIT_ACMODE_HEAT;
        }
    }
}
void set_ac_air_enable(uint8_t state)
{
    if(state != 0)
    {
        if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_AIR) == 0)
        {
            ctrler.reg[REG_ACMODE_SELECT].val |= BIT_ACMODE_AIR;
        }
    }
    else
    {
        if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_AIR) != 0)
        {
            ctrler.reg[REG_ACMODE_SELECT].val &= ~BIT_ACMODE_AIR;
        }
    }
}
void set_ac_dry_enable(uint8_t state)
{
    if(state != 0)
    {
        if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_DRY) == 0)
        {
            ctrler.reg[REG_ACMODE_SELECT].val |= BIT_ACMODE_DRY;
        }
    }
    else
    {
        if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_DRY) != 0)
        {
            ctrler.reg[REG_ACMODE_SELECT].val &= ~BIT_ACMODE_DRY;
        }
    }
}
void set_machine_enable(uint8_t type)
{
    if(ctrler.reg[REG_SYSTEM_SELECT].val != type)
    {
        ctrler.reg[REG_SYSTEM_SELECT].val = type;
    }
}
void set_panel_luminance(uint8_t level)
{
    if(ctrler.luminance != level)
    {
        ctrler.luminance = level;
        panel_luminance_set(level);
    }
}
void set_low_power(uint8_t state)
{
    if(ctrler.lowPower != state)
    {
        ctrler.lowPower = state;
    }
    if(state == 0)
    {
        ctrler.timer = 0;
    }
}
void set_icon_blink(uint8_t id, uint8_t state)
{
    if(state != 0)
    {
        panel_icon_blink_set(id, 7);
    }
    else
    {
        panel_icon_blink_set(id, 0);
    }
}
void set_mode_blink(uint8_t state)
{
    switch(get_system())
    {
        case SYSTEM_AC:
        {
            switch(get_ac_mode())
            {
                case ACMODE_AIR:
                {
                    set_icon_blink(ICON_ACMODE_AIR, state);
                }
                break;
                case ACMODE_COOL:
                {
                    set_icon_blink(ICON_ACMODE_COOL, state);
                }
                break;
                case ACMODE_HEAT:
                {                    
                    set_icon_blink(ICON_ACMODE_HEAT, state);
                }
                break;
                case ACMODE_DRY:
                {
                    set_icon_blink(ICON_ACMODE_DRY, state);
                }
                break;
            }   
        }
        break;
        case SYSTEM_DN:
        {
            set_icon_blink(ICON_MODE_DN, state);
        }
        break;
        case SYSTEM_FRESH:
        {
            set_icon_blink(ICON_MODE_FRESH, state);
        }
        break;
    }
}
void set_select_state(uint8_t state)
{
    if(ctrler.menuSelected != state)
    {
        ctrler.menuSelected = state;
    }
}
void set_speed_delay(uint32_t delay)
{
    if(ctrler.speedDelay != delay)
    {
        ctrler.speedDelay = delay;
    }
}

void set_tempSet_dn(uint8_t temp)
{
    if(ctrler.reg[REG_TEMP_SET_DN].val != temp)
    {
        ctrler.reg[REG_TEMP_SET_DN].val = temp;
    }
}
void set_power_dn(uint8_t state)
{
    if(state != 0)
    {
        if((ctrler.reg[REG_POWER_STATE].val & BIT_POWER_DN) == 0)
        {
            ctrler.reg[REG_POWER_STATE].val |= BIT_POWER_DN;
        }
    }
    else
    {
        if((ctrler.reg[REG_POWER_STATE].val & BIT_POWER_DN) != 0)
        {
            ctrler.reg[REG_POWER_STATE].val &= ~BIT_POWER_DN;
        }
    }
}
void set_power_fresh(uint8_t state)
{
    if(state != 0)
    {
        if((ctrler.reg[REG_POWER_STATE].val & BIT_POWER_FRESH) == 0)
        {
            ctrler.reg[REG_POWER_STATE].val |= BIT_POWER_FRESH;
        }
    }
    else
    {
        if((ctrler.reg[REG_POWER_STATE].val & BIT_POWER_FRESH) != 0)
        {
            ctrler.reg[REG_POWER_STATE].val &= ~BIT_POWER_FRESH;
        }
    }
}
void set_speedSet_fresh(uint8_t speed)
{
    if(ctrler.reg[REG_SPEED_SET_FRESH].val != speed)
    {
        ctrler.reg[REG_SPEED_SET_FRESH].val = speed;
    }
}
void set_out_speed_fresh(uint8_t speed)
{
    if(ctrler.reg[REG_SPEED_OUT_FRESH].val != speed)
    {
        ctrler.reg[REG_SPEED_OUT_FRESH].val = speed;
    }
}
void set_controller_dn_step(uint8_t step)
{
    if(ctrler.dnStep != step)
    {
        ctrler.dnStep = step;
    }
}
void reset_controller_dn(void)
{
    if(ctrler.dnStep != 0)
    {
        ctrler.dnStep = 0;
    }
}
void set_unit(uint8_t unit)
{
    if(ctrler.reg[REG_TEMP_UNIT].val != unit)
    {
        ctrler.reg[REG_TEMP_UNIT].val = unit;
    }
    panel_unit_set(unit);
}
void set_save(uint8_t state)
{
    if(ctrler.save != state)
    {
        ctrler.save = state;
    }
}
void set_fault(uint8_t error)
{
    if(ctrler.fault != error)
    {
        ctrler.fault = error;
    }
}


void system_wake_up(void)
{
    ctrler.timer = 0;
    set_low_power(0);
    set_save(1);
    set_btn_light(BTN_LIGHT_POWER, 1);
    set_btn_light(BTN_LIGHT_PAGE, 1);
    set_btn_light(BTN_LIGHT_OTHER, 1);
    panel_luminance_set(get_luminance());
}

uint8_t get_page(void)
{
    return ctrler.page;
}
uint8_t get_device_addr(void)
{
    return ctrler.deviceAddr;
}
uint8_t get_baudrate(void)
{
    return ctrler.baudrate;
}
uint8_t get_luminance(void)
{
    return ctrler.luminance;
}
uint8_t get_low_power_state(void)
{
    return ctrler.lowPower;
}
uint8_t get_relay_mode(void)
{
    return ctrler.relayMode;
}
uint8_t get_net_state(void)
{
    return ctrler.net;
}
uint8_t get_lock_state(void)
{
    return ctrler.reg[REG_BTN_LOCK].val;
}
uint8_t get_system(void)
{
    return ctrler.reg[REG_SYSTEM].val;
}
uint8_t get_power_ac(void)
{
    uint8_t state = 0;
    if((ctrler.reg[REG_POWER_STATE].val & BIT_POWER_AC) != 0)
    {
        state = 1;
    }
    return state;
}
uint8_t get_controller_ac_step(void)
{
    return ctrler.acStep;
}
uint32_t get_output_speed_delay(void)
{
    return ctrler.speedDelay;
}
uint8_t get_ac_mode(void)
{
    return ctrler.reg[REG_MODE_AC].val;
}
uint8_t get_ac_mode_select(void)
{
    return ctrler.reg[REG_ACMODE_SELECT].val;
}
uint8_t get_speedSet_ac(void)
{
    return ctrler.reg[REG_SPEED_SET_AC].val;
}
uint8_t get_eco_state(void)
{
    return ctrler.reg[REG_ECO].val;
}
uint8_t get_tempSet_ac(void)
{
    return ctrler.reg[REG_TEMP_SET_AC].val;
}
uint8_t get_temp_upLimit(void)
{
    return ctrler.reg[REG_TEMP_SET_LIMIT_UP].val;
}
uint8_t get_temp_downLimit(void)
{
    return ctrler.reg[REG_TEMP_SET_LIMIT_DOWN].val;
}
uint8_t get_valve_state_ac(void)
{
    uint8_t state = 0;
    if((ctrler.reg[REG_VALVE_STATE].val & BIT_VALVE_AC) != 0)
    {
        state = 1;
    }
    return state;
}
uint8_t get_valve_state_dn(void)
{
    uint8_t state = 0;
    if((ctrler.reg[REG_VALVE_STATE].val & BIT_VALVE_DN) != 0)
    {
        state = 1;
    }
    return state;
}
uint8_t get_tempSmp(void)
{
    return ctrler.reg[REG_TEMP_SMP].val + get_calib_signed();
}
uint8_t get_out_speed_ac(void)
{
    return ctrler.reg[REG_SPEED_OUT_AC].val;
}
uint8_t get_btn_light_state(uint8_t id)
{
    return ctrler.btnLight[id];
}
uint8_t get_menu(void)
{
    return ctrler.menu;
}
uint8_t get_calib_unsigned(void)
{
    return ctrler.reg[REG_CALIB].val;
}
signed char get_calib_signed(void)
{
    signed char calib;
    if((ctrler.reg[REG_CALIB].val & 0x80) != 0)
    {
        calib = -(signed char)(ctrler.reg[REG_CALIB].val & 0x7F);
    }
    else
    {
        calib = (signed char)ctrler.reg[REG_CALIB].val;
    }
    return calib;
}
uint8_t get_speed_ctrl_state(void)
{
    return ctrler.reg[REG_SPEED_CTRL].val;
}
uint8_t get_low_temp_protect_state(void)
{
    return ctrler.reg[REG_LOW_TEMP_PROTECT].val;
}
uint8_t get_memory_mode_state(void)
{
    return ctrler.reg[REG_MEMORY].val;
}
uint8_t get_backlash_up(void)
{
    return ctrler.reg[REG_BACKLASH_UP].val;
}
uint8_t get_backlash_down_unsigned(void)
{
    return ctrler.reg[REG_BACKLASH_DOWN].val;
}
signed char get_backlash_down_signed(void)
{
    signed char val;
    if((ctrler.reg[REG_BACKLASH_DOWN].val & 0x80) != 0)
    {
        val = -(signed char)(ctrler.reg[REG_BACKLASH_DOWN].val & 0x7F);
    }
    else
    {
        val = (signed char)ctrler.reg[REG_BACKLASH_DOWN].val;
    }
    return val;
}
uint8_t get_tempSet_upLimit(void)
{
    return ctrler.reg[REG_TEMP_SET_LIMIT_UP].val;
}
uint8_t get_tempSet_downLimit(void)
{
    return ctrler.reg[REG_TEMP_SET_LIMIT_DOWN].val;
}
uint8_t get_ac_cool_enable(void)
{
    uint8_t state = 0;
    if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_COOL) != 0)
    {
        state = 1;
    }
    return state;
}
uint8_t get_ac_heat_enable(void)
{
    uint8_t state = 0;
    if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_HEAT) != 0)
    {
        state = 1;
    }
    return state;
}
uint8_t get_ac_air_enable(void)
{
    uint8_t state = 0;
    if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_AIR) != 0)
    {
        state = 1;
    }
    return state;
}
uint8_t get_ac_dry_enable(void)
{
    uint8_t state = 0;
    if((ctrler.reg[REG_ACMODE_SELECT].val & BIT_ACMODE_DRY) != 0)
    {
        state = 1;
    }
    return state;
}
uint8_t get_machine_enable(void)
{
    return ctrler.reg[REG_SYSTEM_SELECT].val;
}
uint8_t get_panel_luminance(void)
{
    return ctrler.luminance;
}
uint8_t get_selected_state(void)
{
    return ctrler.menuSelected;
}
uint32_t get_speed_delay(void)
{
    return ctrler.speedDelay;
}
uint8_t get_tempSet_dn(void)
{
    return ctrler.reg[REG_TEMP_SET_DN].val;
}
uint8_t get_power_dn(void)
{
    uint8_t state = 0;
    if((ctrler.reg[REG_POWER_STATE].val & BIT_POWER_DN) != 0)
    {
        state = 1;
    }
    return state;
}
uint8_t get_power_fresh(void)
{
    uint8_t state = 0;
    if((ctrler.reg[REG_POWER_STATE].val & BIT_POWER_FRESH) != 0)
    {
        state = 1;
    }
    return state;
}
uint8_t get_speedSet_fresh(void)
{
    return ctrler.reg[REG_SPEED_SET_FRESH].val;
}
uint8_t get_out_speed_fresh(void)
{
    return ctrler.reg[REG_SPEED_OUT_FRESH].val;
}
uint8_t get_controller_dn_step(void)
{
    return ctrler.dnStep;
}
uint8_t get_unit(void)
{
    return ctrler.reg[REG_TEMP_UNIT].val;
}
uint8_t get_save(void)
{
    return ctrler.save;
}
void get_ctrler_regs(Regs **pReg)
{
    *pReg = (Regs *)(&ctrler);
}
uint8_t get_fault(void)
{
    return ctrler.fault;
}
/* 低功耗超时处理函数 */
void low_power_process(void)
{
    ctrler.timer++;
    if(ctrler.timer == 15)
    {
        if(get_fault() == 0)
        {
            set_low_power(1);
            set_page(PAGE_SYSTEM);
            set_menu(MENU_CALIB);
            panel_luminance_set(LUMINANCE_LOW_POWER);
            panel_tubex_blink_set(2, 0); 
            panel_unitx_blink_set(2, 0); 
            set_select_state(0);
            set_btn_light(BTN_LIGHT_POWER, 1);
            set_btn_light(BTN_LIGHT_OTHER, 0);
            if(get_machine_enable() >= SYSTEM_AC_DN)
            {
                set_btn_light(BTN_LIGHT_PAGE, 1);
            }
            else
            {
                set_btn_light(BTN_LIGHT_PAGE, 0);
            }
        }
    }
}

void unit_convert(void)
{
    uint8_t temp_upLimit = get_temp_upLimit();
    uint8_t temp_downLimit = get_temp_downLimit();
    uint8_t tempSet_ac = get_tempSet_ac();
    uint8_t tempSet_dn = get_tempSet_dn();
    uint8_t temp_unit = get_unit();
    
    if(temp_unit == UNIT_C)
    {
        temp_upLimit = (temp_upLimit - 32) / 1.8f;
        temp_downLimit = (temp_downLimit - 32) / 1.8f;
        tempSet_ac = (tempSet_ac - 32) / 1.8f;
        tempSet_dn = (tempSet_dn - 32) / 1.8f;
    }
    else
    {
        temp_upLimit = temp_upLimit * 1.8f + 32;
        temp_downLimit = temp_downLimit * 1.8f + 32;
        tempSet_ac = tempSet_ac * 1.8f + 32;
        tempSet_dn = tempSet_dn * 1.8f + 32;
    }
    set_temp_upLimit(temp_upLimit);
    set_temp_downLimit(temp_downLimit);
    set_tempSet_ac(tempSet_ac);
    set_tempSet_dn(tempSet_dn);
}
void controller_init(void)
{
    sys_params_init();
    set_mode_blink(1);
    set_btn_light(BTN_LIGHT_POWER, 1);
    set_btn_light(BTN_LIGHT_PAGE, 1);
    set_btn_light(BTN_LIGHT_OTHER, 1);
}
